package Browser;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import crawler.src.Filter;

public class Definitions extends JFrame{
	
	final static long serialVersionUID = 12345678L; 
	
	private Filter filter;
	
	private JPanel geralPanel = new JPanel(new GridLayout(6, 0));
	private JPanel securityPanel = new JPanel(new GridLayout(2, 0));
	private JPanel titlePanel = new JPanel(new GridLayout(1,0));
	private JPanel controlPanel = new JPanel(new GridLayout(2, 2));
	private JPanel wordTitle = new JPanel(new GridLayout(1, 1));
	private JPanel wordPanel = new JPanel(new GridLayout(1, 2));
	
	private JLabel l0 = new JLabel("Choose a security level", SwingConstants.CENTER);
	private JLabel l1 = new JLabel("Choose a Subject to Block", SwingConstants.CENTER);
	private JLabel l2 = new JLabel("Blocked Key Words", SwingConstants.CENTER);
	private JTextField kywTF = new JTextField("", 35);
	private JTextField rkwTF = new JTextField("", 35);
	private JLabel blockedW = new JLabel();
	private JButton okB = new JButton("Add");
	private JButton removeB = new JButton("Remove");
	private JButton changePassB = new JButton("Change Password");
	
	private String subject;
	private static final String securityOpt[] = {"None", "Low", "Medium", "High"};
	
	private static JComboBox<String> securityBox = new JComboBox<String>(securityOpt);
	
	private ArrayList<String> blockedList = new ArrayList<String>();	
	
	public Definitions() {
		
		setTitle("Definitions");
		setDefaultLookAndFeelDecorated(true);
		setSize(500, 500);
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		
		try {
			filter = new Filter();
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		loadWords();
		
		okB.addActionListener(new ActionListener() {
		      public void actionPerformed(ActionEvent e) {
		    	  addKey();
		      }
		});
		
		removeB.addActionListener(new ActionListener() {
		      public void actionPerformed(ActionEvent e) {
		    	  removeKey();
		      }
		    });
		
		kywTF.addActionListener(new ActionListener() {
		      public void actionPerformed(ActionEvent e) {
		    	  addKey();
		      }
		    });
		
		rkwTF.addActionListener(new ActionListener() {
		      public void actionPerformed(ActionEvent e) {
		    	  removeKey();
		      }
		    });
		
		securityBox.addActionListener(new ActionListener() {
		      public void actionPerformed(ActionEvent e) {
		    	  try {
					filter.cleanRecord();
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
		      }
		    });
		
		changePassB.addActionListener(new ActionListener() {
		      
			public void actionPerformed(ActionEvent e) {
				
					filter.deletePass();
					
					Password password = new Password();
					dispose();
					
		      }
		    });
		
		JScrollPane scroller = new JScrollPane(blockedW, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
		
		securityPanel.setBackground(Color.WHITE);
		securityPanel.add(l0);
		securityPanel.add(securityBox);
		
		titlePanel.setBackground(Color.WHITE);
		titlePanel.add(l1);
		
		controlPanel.setBackground(Color.WHITE);
		controlPanel.add(kywTF);
		controlPanel.add(okB);
		controlPanel.add(rkwTF);
		controlPanel.add(removeB);
		
		wordTitle.setBackground(Color.WHITE);
		wordTitle.add(l2);
		
		wordPanel.setBackground(Color.WHITE);
		wordPanel.add(scroller, BorderLayout.CENTER);
		
		geralPanel.setBackground(Color.WHITE);
		geralPanel.add(securityPanel);
		geralPanel.add(titlePanel);
		geralPanel.add(controlPanel);
		geralPanel.add(wordTitle);
		geralPanel.add(wordPanel);
		geralPanel.add(changePassB);
		
		add(geralPanel);
		setVisible(true);
	}
	
	public Filter getFilter() {
		return this.filter;
	}
	
	public static String getSecurityItem() {
		return securityBox.getSelectedItem().toString();
	}
	
	private void loadWords() {
		String text = "<html><font color=\"red\">";
		
		blockedList = filter.getKeyWords();
		  
		for (String w : blockedList) {
			text += "- " + w + "<br>";
		}
		
		text += "</font></html>";
		
		blockedW.setText(text);
	}
	
	private void addKey() {
		String text = "<html><font color=\"red\">";
 	   	subject = kywTF.getText();
 	   
 	   	try {
 		   	filter.setKeyword(subject);
 		   	blockedList = filter.getKeyWords();
		  
 		   	for (String w : blockedList) {
 		   		text += "- " + w + "<br>";
 		   	}
 		   	text += "</font></html>";
 		   	blockedW.setText(text);
 		   	kywTF.setText("");
 	   	}
 	   	catch(SQLException sqle){
    		   System.out.println("Error in DataBase");
 	   	}
	}
	
	private void removeKey() {
		try {
			filter.remKyeWord(rkwTF.getText());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		String text = "<html><font color=\"red\">";
		
		blockedList = filter.getKeyWords();
		  
		for (String w : blockedList) {
			text += "- " + w + "<br>";
		}
		
		text += "</font></html>";
		
		blockedW.setText(text);
		rkwTF.setText("");
	}
}
